﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ASPNETVN.PORTAL.Components.Security;
using ASPNETVN.PORTAL.Components.Configuration.Items;

namespace ASPNETVN.PORTAL.Masters
{
    public partial class Blank : System.Web.UI.MasterPage
    {
        public bool ShowTabs = true;

        protected override void OnInit(EventArgs e)
        {
            // Obtain Portal from Current Context
            Components.Configuration.Items.Portal portal = (Components.Configuration.Items.Portal)HttpContext.Current.Items["Portal"];

            // Title of web site.
            this.Head.Title = portal.Title;


            // If user logged in, customize welcome message
            if (Request.IsAuthenticated)
            {
                WelcomeMessage.Text = "<a href='" + Global.GetApplicationPath(Request) + "/Default.aspx?page=ChangePassword' class='SiteLink'>" + Global.RM.GetString("SYSTEM_WELCOME") + " " + PortalSecurity.CurrentUser.Identity.Email + "</a> <span>|</span>";
                WelcomeMessage.ToolTip = PortalSecurity.CurrentUser.Identity.ToString();

                // if authentication mode is Cookie, provide a logoff link
                if (Context.User.Identity.AuthenticationType == "Forms")
                {
                    LogoffLink.Text = "<a href='" + Global.GetApplicationPath(Request) + "/Admin/Logoff.aspx' class=SiteLink> " + Global.RM.GetString("SYSTEM_LOGOUT") + "</a>";
                }

            }
            else
            {
                // Allow register?
                if (portal.AllowRegister)
                {
                    LogoffLink.Text = "<a href='" + Global.GetApplicationPath(Request) + "/Register.aspx' class=SiteLink> " + Global.RM.GetString("SYSTEM_REGISTER") + "</a> <span>|</span> " + "<a href='" + Global.GetApplicationPath() + "/SignIn.aspx' class=SiteLink> " + Global.RM.GetString("SYSTEM_SIGNIN") + "</a>";
                }
                else
                {
                    LogoffLink.Text = "<a href='" + Global.GetApplicationPath(Request) + "/SignIn.aspx' class=SiteLink> " + Global.RM.GetString("SYSTEM_SIGNIN") + "</a>";
                }
            }

            // Dynamically render portal tab strip
            if (Page.IsPostBack == false)
            {
                if (ShowTabs)
                {
                    for (int i = 0; i < portal.Tabs.Count; i++)
                    {
                        Tab tab = (Tab)portal.Tabs[i];
                        if (PortalSecurity.IsInRoles(tab.AuthorizedRoles) || PortalSecurity.IsInRole("1"))
                        {
                            if (tab.ShowOnMenu || PortalSecurity.IsInRole("1"))
                            {
                                mnuSiteNavigation.Items.Add(new MenuItem(tab.Name, tab.ID.ToString(), "", "~/Default.aspx?tabid=" + tab.ID));
                                mnuSiteNavigation.Items.Add(new MenuItem("|", "", "", ""));
                            }
                        }
                    }
                    mnuSiteNavigation.Items.RemoveAt(mnuSiteNavigation.Items.Count - 1);
                }
            }

            base.OnInit(e);
        }

        protected void Page_Load(object sender, EventArgs e)
        {

        }
    }
}
